package com.heihei.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.heihei.domain.SiteType;
import com.heihei.domain.User;
import com.heihei.service.SiteTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/type")
public class SiteTypeController {
    @Autowired
    private SiteTypeService siteTypeService;

    @RequestMapping("/atypes")
    @ResponseBody
    public String selectAllTypes(HttpSession session) {
        //获取session中的登录用户User
        User currentUser = (User) session.getAttribute("currentUser");

        List<SiteType> siteTypes = siteTypeService.selectAllTypes(currentUser);

        SiteType siteType = new SiteType();
        siteType.setId(0);
        siteType.setType("全部");
        siteTypes.add(0, siteType);

        ObjectMapper objectMapper = new ObjectMapper();
        String s = null;
        try {
            s = objectMapper.writeValueAsString(siteTypes);
        } catch (JsonProcessingException e) {
            System.out.println("类型数据转换字符串时出了问题");
            e.printStackTrace();
        }
//        System.out.println(s);
        return s;
    }

}
